package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyTravelQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.JSONUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/strategyTravel")
public class StrategyTravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyService strategyService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyTravelQueryObject qo) {
        PageInfo pageInfo = travelService.selectTravelByStrategyId(qo);
        model.addAttribute("pageInfo",pageInfo);
        List<Strategy> strategies = strategyService.selectAll();
        model.addAttribute("strategies",strategies);
        List<Travel> existTravels = travelService.selectTravelByMid();
        List<Travel> travels = travelService.selectAll();
        model.addAttribute("travels",travels);
        model.addAttribute("existTravels",existTravels);

        return "strategyTravel/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object deleteRelation(Long id){
        JSONResult json = new JSONResult();
        try{
            travelService.deleteRelation(id);
        }catch (Exception e){
            e.printStackTrace();
            json.mark(e.getMessage());
        }



        return json;
    }


    @RequestMapping("/save")
    @ResponseBody
    public  Object save(Long strategyId,Long travelId){
        JSONResult json = new JSONResult();
        try{
            travelService.insertRelation(strategyId,travelId);
        }catch (Exception e){
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
}
